package com.winit.generator.handler.impl;

import com.winit.generator.Constants;
import com.winit.generator.config.Configuration;
import com.winit.generator.handler.BaseHandler;
import com.winit.generator.model.EntityInfo;

import java.io.File;
import java.util.Map;

public class FilterHandler extends BaseHandler<EntityInfo> {

    public FilterHandler(String ftlName, EntityInfo info){
        this.ftlName = ftlName;
        this.info = info;
        this.savePath = Configuration.getString("base.baseDir") + File.separator
                        + Configuration.getString("filter.path") + File.separator + info.getFilterName()
                        + Constants.FILE_SUFFIX_JAVA;

    }

    @Override
    public void combileParams(EntityInfo entityInfo) {
        this.param.put("packageStr", entityInfo.getEntityPackage());
        this.param.put("filterPackageStr", entityInfo.getFilterPackage());
        StringBuilder sb = new StringBuilder();
        for (String str : entityInfo.getImports()) {
            sb.append("import ").append(str).append(";\r\n");
        }
        this.param.put("importStr", sb.toString());
        this.param.put("entityDesc", entityInfo.getEntityDesc());
        this.param.put("className", entityInfo.getClassName());
        this.param.put("filterName", entityInfo.getFilterName());

        // 为数字和日期类型添加查询条件
        sb = new StringBuilder();
        // 字段对应属性名称
        Map<String, String> propRemarks = entityInfo.getPropRemarks();
        for (Map.Entry<String, String> entry : entityInfo.getPropTypes().entrySet()) {
            String propName = entry.getKey();
            String propType = entry.getValue();

            if ("gmtModified".equals(propName)) {
                continue;
            }

            // 数字类型
            if ("BigDecimal".equals(propType)) {
                // 注释、类型、名称
                sb.append("    /**")
                        .append("\r\n     * ")
                        .append(propRemarks.get(propName)).append("- 条件符号")
                        .append("\r\n     */\r\n")
                        .append("    private ")
                        .append("String")
                        .append(" ")
                        .append(propName).append("Symbol")
                        .append(";\r\n");
                continue;
            }

            // 日期类型
            if ("Date".equals(propType)) {

                // 开始时间
                sb.append("    /**")
                        .append("\r\n     * ")
                        .append(propRemarks.get(propName)).append("- 开始时间")
                        .append("\r\n     */\r\n")
                        .append("    private ")
                        .append("String")
                        .append(" ")
                        .append(propName).append("Begin")
                        .append(";\r\n");

                // 开始时间
                sb.append("    /**")
                        .append("\r\n     * ")
                        .append(propRemarks.get(propName)).append("- 结束时间")
                        .append("\r\n     */\r\n")
                        .append("    private ")
                        .append("String")
                        .append(" ")
                        .append(propName).append("End")
                        .append(";\r\n");
            }

        }

        this.param.put("filterPropertiesStr", sb.toString());
    }
}
